package com.jml.controller;

import com.jml.bean.Purchase;
import com.jml.bean.Stock;
import com.jml.service.PurchaseService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/*
 * @Author yaxiongliu
 **/
@RestController
public class PurchaseController {

    @Autowired
    private PurchaseService purchaseService;
    @Autowired
    private RestTemplate restTemplate;

    @GlobalTransactional//TM 事务的管理器
    @RequestMapping("/purchase/add/{deno}")
    public boolean addPurchaseHandle(@RequestBody Purchase purchase
            ,@PathVariable("deno") int deno) {
        purchaseService.addPurchase(purchase);
        int i = 3 / deno;
        Stock stock = new Stock();
        stock.setName(purchase.getName());
        stock.setTotal(purchase.getCount());
        String url = "http://msc-stock/stock/add";
        Boolean result = restTemplate.postForObject(url, stock, Boolean.class);
        if (result != null) {
            return result;
        }
        return false;
    }

}
